﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace WindowsTheme.Helper
{
    class XmlHelper
    {/// <summary>
    /// 创建xml的帮助类  当前版本未使用
    /// </summary>
    /// <param name="FilePath"></param>
    /// <param name="FileName"></param>
        public void CreateXmlFile(string FilePath,string FileName)
        {
            XmlDocument xmlDoc = new XmlDocument();
            //创建类型声明节点  
            XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
            xmlDoc.AppendChild(node);
            //创建根节点  
            XmlNode root = xmlDoc.CreateElement("UserInfo");
            xmlDoc.AppendChild(root);
            CreateNode(xmlDoc, root, "UserName", "xuwei");
            CreateNode(xmlDoc, root, "sex", "male");
            CreateNode(xmlDoc, root, "age", "25");
            try
            {
                xmlDoc.Save(FilePath+ FileName);
            }
            catch (Exception e)
            {
               
                PrintLog.Log(e.Message);
            }
        

        }
        /// <summary>    
        /// 创建节点    
        /// </summary>    
        /// <param name="xmldoc"> xml文档  </param> 
        /// <param name="parentnode">父节点  </param>  
        /// <param name="name">节点名  </param>  
        /// <param name="value"> 节点值 </param>  
        ///   
        public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
        {
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
            node.InnerText = value;
            parentNode.AppendChild(node);
        }

        /// <summary>
        /// 获取xml的内容
        /// </summary>
        /// <param name="XmlValue">xml的文本信息</param>
        /// <param name="XmlPath">xpath路径</param>
        /// <returns></returns>
        public static List<string> SelectNode(string XmlValue,String XmlPath)
        {
            List<String> ValueList = new List<string>();
            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.LoadXml(XmlValue);
                //取根结点
                XmlElement root = xmlDoc.DocumentElement;//取到根结点


                //取指定的结点的集合
                XmlNodeList nodes = xmlDoc.SelectNodes(XmlPath);


                foreach (XmlNode line in nodes)
                {
                    ValueList.Add(line.InnerText);
                }
            }
            catch (Exception ex) {
                PrintLog.Log(ex);
            }
            return ValueList;
        }
        /// <summary>
        /// 保存xml的帮助类，成功返回true
        /// </summary>
        /// <param name="XmlValue">xml的文本信息</param>
        /// <param name="XmlPath">保存xml的文件路径</param>
        /// <returns></returns>
        public static bool SaveXml(String XmlValue, String XmlPath) {
            try {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(XmlValue);
                xml.Save(XmlPath);
                return true;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                return false;
            }
        }
        /// <summary>
        /// 保存xml的帮助类，成功返回true
        /// </summary>
        /// <param name="XmlValue">XmlDocument的类</param>
        /// <param name="XmlPath">保存xml的文件路径</param>
        /// <returns></returns>
        public static bool SaveXml(XmlDocument XmlValue, String XmlPath)
        {
            try
            {
               
                XmlValue.Save(XmlPath);
                return true;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                return false;
            }
        }
       /// <summary>
       /// xml替换内容值的方法，需要xml文件的值，xpath
       /// </summary>
       /// <param name="XmlValue">xml文件的值</param>
       /// <param name="XPath">选择路径</param>
       /// <param name="NewValue">替换的值</param>
       /// <returns></returns>
        public static XmlDocument ModifyXml(String XmlValue, String XPath,String NewValue) {
            XmlDocument XmlDoc = new XmlDocument();
            try
            {
             
                XmlDoc.LoadXml(XmlValue);
             
                //取根结点
                XmlElement root = XmlDoc.DocumentElement;//取到根结点


                //取指定的结点的集合
                XmlNodeList nodes = XmlDoc.SelectNodes(XPath);


                foreach (XmlNode line in nodes)
                {
                    line.InnerText = NewValue;
                }
             
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                
            }
            return XmlDoc;
        }


        public static XmlDocument ModifyXml(XmlDocument XmlDoc, String XPath, String NewValue)
        {
        
            try
            {

              

                //取根结点
                XmlElement root = XmlDoc.DocumentElement;//取到根结点


                //取指定的结点的集合
                XmlNodeList nodes = XmlDoc.SelectNodes(XPath);


                foreach (XmlNode line in nodes)
                {
                    line.InnerText = NewValue;
                }

            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);

            }
            return XmlDoc;
        }
        /// <summary>
        /// 返回xml匹配的第一个值
        /// </summary>
        /// <param name="ValueList"></param>
        /// <returns></returns>
        public static string SelectFirstValue(List<String> ValueList) {
            string Value = "";
            foreach (String line in ValueList) {
                Value = line;
                break;
            }
            return Value;
        }
    }
}
